<?php

	class MediaModel extends ObjectModel
	{
		public function __construct()
		{
			parent::__construct("md_media","md_id");
		}
				
		public function getColumns()
		{
			return array(
				new EbTableColumn("md_id","int",true,0x01,true),
				new EbTableColumn("ob_id","int"),
				new EbTableColumn("teaser","text",false),
				new EbTableColumn("fd_id","int")
			);
		}
				
		public function selectByFolder($folder = 1)
		{
			$this->vomodulefile = null;
			
			$media_folder = EbModel::getModel('Media','MediaFolder')->getFolderPath($folder);
			
			$media_provider = new Media_HttpProvider('','/',$media_folder);
			
			$sql = 'SELECT *,"" AS urlthumbnail,"" AS urlmain FROM md_media
					INNER JOIN eb_object ON eb_object.ob_id = md_media.ob_id
					WHERE md_media.fd_id = '.$folder;
			
			$listMedias = $this->execute($sql);
			
			$sql = 'SELECT md_item.* FROM md_item
					INNER JOIN md_media ON md_media.md_id = md_item.md_id
					WHERE md_media.fd_id = '.$folder;
			
			$listMediaItems = $this->execute($sql);			
						
			foreach ($listMedias AS $media)
			{
				foreach ($listMediaItems AS $item)
				{
					if ($media->md_id == $item->md_id)
					{
						if ($item->type == 2)
						{
							$media->urlthumbnail = $media_provider->getFileUrl($item->code);
						}
						else if ($item->type == 1)
						{
							$media->urlmain = $media_provider->getFileUrl($item->code);
						}
					}
				}
			}
			
			return $listMedias;
		}
		
		public function selectByFolderForGallery()
		{
			// <!> Dirty <!>
			$f = EbController::$request->getData('file_dir');
			
			$listItems = $this->selectByFolder($f);
			$buffer = '<pics>';
			
			foreach ($listItems AS $item)
			{
				$buffer .= '<pic title="'.$item->name.'" src="'.$item->urlmain.'" />';
			}
			
			return $buffer.'</pics>';
		}		
		
		public function selectByFolderForRotator()
		{
			$listItems = $this->selectByFolder();
			$buffer = '<playlist version="1" xmlns="http://xspf.org/ns/0/"><trackList>';
			
			foreach ($listItems AS $item)
			{
				$buffer .= '<track><title>'.$item->name.'</title><location>'.$item->urlmain.'</location></track>';
			}
			
			return $buffer.'</trackList></playlist>';
		}

	}